<template>
  <div class="ratings" v-el:ratings>
    <div class="ratings-content">
      <div class="overview">
        <div class="overview-left">
          <strong class="score">{{ seller.score}}</strong>
          <div class="title">综合评分</div>
          <div class="rank">高于周边商家{{ seller.rankRate }}%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <star class="star" :size="36" :score="seller.serviceScore"></star>
            <span class="score">{{ seller.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <star class="star" :size="36" :score="seller.foodScore"></star>
            <span class="score">{{ seller.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">送达时间</span>
            <span class="time">{{ seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <split-chunk></split-chunk>
      <div class="comment-wrapper">
        <ratingselect :select-type="selectType" :only-content="onlyContent" 
                    :ratings="ratings" :ratings="ratings"></ratingselect>
        <div class="comment-list">
          <ul>
            <li class="comment-item" v-for="rating in ratings" v-show="needShow(rating.rateType, rating.text)">
              <div class="avatar">
                <img :src="rating.avatar" width="28" height="28" alt="">
              </div>
              <div class="content">
                <span class="username">{{ rating.username }}</span>
                <div class="star-wrap">
                  <star class="star" :size="24" :score="rating.score"></star>
                  <span class="delivery" v-show="rating.deliveryTime">{{ rating.deliveryTime }}分钟送达</span>
                </div>
                <div class="time">{{ rating.rateTime | formatDate }}</div>
                <div class="text" v-show="rating.text">{{ rating.text }}</div>
                <div class="recommend" v-show="rating.recommend && rating.recommend.length">
                  <span class="icon icon-thumb_up"></span>
                  <span class="label" v-for="item in rating.recommend">{{ item }}</span>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Star from 'components/star/star'
  import BScroll from 'better-scroll'
  import Ratingselect from 'components/ratingselect/ratingselect'
  import SplitChunk from 'components/split/split'
  import { formatDate } from 'assets/scripts/date'

  const ERR_NO = 0
  const ALL = 2

  export default {
    props: {
      seller: {
        type: Object
      }
    },
    data() {
      return {
        ratings: [],
        selectType: ALL,
        onlyContent: true
      }
    },
    created() {
      this.$http.get('/api/ratings').then((response) => {
        response = response.body
        if(response.errno === ERR_NO) {
          this.ratings = response.data
          this.$nextTick(() => {
            this.scroll = new BScroll(this.$els.ratings, {
              click: true
            })
          })
        }
      })
    },
    methods: {
     needShow(type, text) {
        if(this.onlyContent && !text) {
          return false
        }
        if(this.selectType === ALL){
          return true
        }
        else {
          return type === this.selectType
        }
      }
    },
    events: {
      'ratingtype.select' (type) {
        this.selectType = type
        this.$nextTick(function() {
          this.scroll.refresh()
        })
      },
      'content.toggle' (content) {
        this.onlyContent = content
        this.$nextTick(function() {
          this.scroll.refresh()
        })
      }
    },
    filters: {
      formatDate(time) {
        let date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd hh:mm')
      }
    },
    components: {
      Star,
      Ratingselect,
      'split-chunk': SplitChunk
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import '../../assets/style/minxin.stylus'
  .ratings
    position: absolute
    left: 0
    bottom: 0
    top: 180px
    width: 100%
    overflow: hidden
    .overview
      display: flex
      width: 100%
      padding: 18px 0
      .overview-left
        flex: 0 0 137px
        border-right: 1px solid rgba(7, 17, 27, .1)
        text-align: center
        @media only screen and (max-width: 320px)
          flex: 0 0 120px 
          width: 120px
        .score
          font-size: 24px
          color: rgb(255, 153, 0)
          line-height: 28px
        .title
          padding: 6px 0 8px 0
          font-size: 12px
          color: rgb(7, 17, 27)
        .rank
          padding-bottom: 6px
          font-size: 10px
          line-height: 10px
          color: rgb(147, 153, 159)
      .overview-right
        flex: 1
        padding-left: 24px
        @media only screen and (max-width: 320px)
          padding-left: 12px
        .score-wrapper
          line-height: 18px
          font-size: 12px
          margin-bottom: 8px
          .star, .title, .score
            display: inline-block
            vertical-align: top
            line-height: 18px
          .star
            margin: 0 12px
            .star-item
              vertical-align: top
            @media only screen and (max-width: 320px)
              margin: 0 3px
          .title
            color: rgb(7, 17, 27)
          .score
            color: rgb(255, 153, 0)
          .time
            margin-left: 12px
            color: rgb(147, 153, 159)
    .comment-wrapper
      padding-left: 18px
      .comment-item
        display: flex
        padding: 18px 0
        border-bottom: 1px solid rgba(1, 17, 27, .1)
        .avatar
          flex: 0 0 28px
          width: 28px
          height: 28px
          margin-right: 12px
          border-radius: 50%
          overflow: hidden
        .content
          position: relative
          flex: 1
          .username
            font-size: 10px
            color: rgb(7, 17, 27)
            line-height: 12px
          .star-wrap
            padding-top: 4px
            vertical-align: top
            .star
              display: inline-block
          .delivery
            vertical-align: top
            margin-left: 6px
            font-size: 10px
            color: rgb(147, 153, 159)
            line-height: 12px
          .time
            position: absolute
            right: 18px
            top: 0
            font-size: 10px
            color: rgb(147, 153, 159)
            line-height: 12px
          .text
            width: 94%
            padding: 6px 0 8px 0
            font-size: 12px
            color: rgb(7, 17, 27)
            line-height: 18px
          .recommend
            width: 94%
            font-size: 0
            line-height: 16px
            .icon, .label
              margin: 0 8px 4px 0
            .icon
              font-size: 12px
              color: rgb(0, 160, 220)
            .label
              padding: 0 6px
              
              font-size: 9px
              color: rgb(147, 153, 159)
              border: 1px solid rgba(7, 17, 27, .1)
              border-radius: 1px
              background-color: #fff
              white-space: wrap
          
</style>
